using UnityEngine;

[AddComponentMenu("NGUI/Examples/Follow Target")]
public class UIFollowTarget : MonoBehaviour
{
	public Transform target;

	public Camera gameCamera;

	public Camera uiCamera;

	public GameObject nick;

	public bool disableIfInvisible = true;

	public bool isVisible = true;

	private Transform mTrans;

	private bool mIsVisible;

	private void Awake()
	{
		mTrans = base.transform;
	}

	private void Start()
	{
		if (target != null)
		{
			if (gameCamera == null)
			{
				gameCamera = NGUITools.FindCameraForLayer(target.gameObject.layer);
			}
			if (uiCamera == null)
			{
				uiCamera = NGUITools.FindCameraForLayer(base.gameObject.layer);
			}
			SetVisible(false);
		}
		else
		{
			Debug.LogError("Expected to have 'target' set to a valid transform", this);
			base.enabled = false;
		}
	}

	private void SetVisible(bool val)
	{
		mIsVisible = val;
		nick.SetActive(val);
	}

	private void LateUpdate()
	{
		if (!(target == null) && !(gameCamera == null))
		{
			if (mIsVisible != isVisible)
			{
				SetVisible(isVisible);
			}
			if (isVisible)
			{
				Vector3 position = gameCamera.WorldToViewportPoint(target.position);
				base.transform.position = uiCamera.ViewportToWorldPoint(position);
				position = mTrans.localPosition;
				position.z = 0f;
				mTrans.localPosition = position;
			}
			OnUpdate(isVisible);
		}
	}

	protected virtual void OnUpdate(bool isVisible)
	{
	}
}
